<?php
if (!defined('APPPATH')) exit('No direct script access allowed');
class usergroup_db extends Model
{
	public $pk = 'usergroup_id';
	public $table = 'ptx_usergroup';

	public function getGroupCache($key){
		$result = pcache('r', 'group_'.$key);
		if(!$result){
			return $this->updateGroupCache($key);
		}
		return $result;
	}

	public function updateGroupCache($type){
		$condition['usergroup_type'] = $type;
		if($sysGroups = $this->findAll($condition)){
			$newGs = array();
			foreach ($sysGroups as $g) {
				if($g['other_permission']){
					$op = unserialize($g['other_permission']);
					foreach ($op as $key=>$v){
						$g[$key] = $v;
					}
				}
				$newGs[$g['usergroup_id']] = $g;
			}
			unset($sysGroups);
			pcache('w','group_'.$type,$newGs);
			return $newGs;
		}
		return false;
	}

	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			if($data['other_permission']){
				$data['other_permission'] = serialize($data['other_permission']);
			}
			$result = $this->create($data);
			return $result;
		}
		return false;
	}

	private function check_value($data){
		if($data['usergroup_key']==null){
			return false;
		}
		return true;
	}

}
